package jaux.tank.service.system.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import jaux.tank.bean.entity.system.User;
import jaux.tank.dao.system.UserRepository;
import jaux.tank.service.system.UserService;
import jaux.tank.utils.DateUtil;
import jaux.tank.utils.PageUtils;
import jaux.tank.utils.factory.Page;

/**
 * Created  on 2018/4/7 0007.
 *
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;
    @Override
    public List<User> findAll(final Map<String, Object> params) {
        return userRepository.findAll(genUserSpec(params));

    }

    @Override
    public Page<User> findPage(Page<User> page, Map<String, Object> params) {
    	Pageable pageable = PageUtils.convertPageToPageable(page);

        org.springframework.data.domain.Page<User> pageResult = userRepository.findAll(genUserSpec(params),pageable);
        page.setTotal(Integer.valueOf(pageResult.getTotalElements()+""));
        page.setRecords(pageResult.getContent());
        return page;
    }
    
    private Specification<User> genUserSpec(Map<String, Object> params){
    	return new Specification<User>(){
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();
                if(params.get("deptid") !=null && StringUtils.isNotEmpty(params.get("deptid").toString())){
                    list.add(criteriaBuilder.equal(root.get("deptid").as(String.class), params.get("deptid").toString()));
                }
                if(StringUtils.isNotBlank((String)params.get("roleId"))){
                	list.add(criteriaBuilder.or(criteriaBuilder.like(root.get("roleid"), params.get("roleId")+","),
                			criteriaBuilder.like(root.get("roleid"),","+ params.get("roleId"))));
                }
                if(params.get("name") !=null && StringUtils.isNotEmpty(params.get("name").toString())){
                    list.add(criteriaBuilder.equal(root.get("name").as(String.class), params.get("name").toString()));
                }
                if(params.get("nameLike")!=null && StringUtils.isNotEmpty(params.get("nameLike").toString())){
                	String likePattern = "%"+params.get("nameLike")+"%";
					Predicate nameLike = criteriaBuilder.like(root.get("name").as(String.class), likePattern);
                	Predicate accountLike = criteriaBuilder.like(root.get("account").as(String.class), likePattern);
                	Predicate phoneLike = criteriaBuilder.like(root.get("phone").as(String.class), likePattern);
					list.add(criteriaBuilder.or(nameLike, accountLike, phoneLike));
                }
                if(params.get("beginTime") != null && StringUtils.isNotEmpty(params.get("beginTime").toString())){
                    list.add(criteriaBuilder.greaterThan(root.get("createtime").as(Date.class), DateUtil.parseDate(params.get("beginTime").toString())));
                }
                if(params.get("endTime") != null && StringUtils.isNotEmpty(params.get("endTime").toString())){
                    list.add(criteriaBuilder.lessThan(root.get("createtime").as(Date.class),DateUtil.parseDate(params.get("endTime").toString())));
                }


                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }
        };
    }
}
